import os
import time
import sys
import jieba
from flask import Flask, request, jsonify, Response
from flask_cors import CORS
import subprocess
sys.path.append(r".//common")
from func_gpt import load_llm, load_retriever, llmresponse  # 确保这部分代码和路径是正确的
import fitz  # PyMuPDF
import docx
import io
import hashlib
import hmac
import base64
from datetime import datetime
from werkzeug.utils import secure_filename
FOLDERS_TO_CLEAR = [
    'docs11111/cleaned_json',
    'docs11111/cleaned_txt',
    'docs11111/docx',
    'docs11111/json',
]

def clear_folders(folders):
    for folder in folders:
            for filename in os.listdir(folder):
                file_path = os.path.join(folder, filename)
                try:
                    if os.path.isfile(file_path) or os.path.islink(file_path):
                        os.remove(file_path)
                    # 如果没有子目录，可以省略对目录的处理
                except Exception as e:
                    print(f'Failed to delete {file_path}. Reason: {e}')
#clear_folders(FOLDERS_TO_CLEAR)

scripts_to_run = [
    'common/clean_data.py',
    'common/txt2json.py',
    'common/write_abstract.py',
    'common/vector_store.py',
]
for script in scripts_to_run:
    try:
        result = subprocess.run(['python', script], check=True, capture_output=True, text=True)
        print(f"Output of {script}: {result.stdout}")
    except subprocess.CalledProcessError as e:
        print(f"Error running {script}: {e.stderr}")
    #不对就报错


#clear_folders(FOLDERS_TO_CLEAR)